package com.jxd.project.travel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jxd.project.travel.service.IDictionaryService;
import com.jxd.project.travel.dto.DictionaryQueryDTO;
import com.jxd.project.travel.model.DictType;
import com.jxd.project.travel.model.Dictionary;
import com.jxd.project.travel.service.IDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminController
 * @Description TODO
 * @Author 邢子剑
 * @Date 2025/9/24 上午9:13
 * @Version 1.0
 */
@RestController
public class AdminController {
    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private IDictTypeService dictTypeService;

    @Autowired
    private ObjectMapper objectMapper; // Spring 自动注入 Jackson 的 ObjectMapper

    /**
     * 获取全部出行方式
     * @return
     */
    @RequestMapping("/getTransportation")
    public List<Map<Integer, String>> getTransportation(){
        return dictionaryService.getTransportation();
    }

    /**
     * 获取审核状态
     * @return
     */
    @RequestMapping("/getApproval")
    public List<Map<String, String>> getApproval(){
        return dictionaryService.getApproval();
    }


    @RequestMapping("/getOneDictionary")
    public Dictionary getOneDictionary(int id){
        return dictionaryService.getOne(id);
    }


    @RequestMapping("/getAllDictType")
    public IPage<DictType> getAllDictType(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam String type){
        Page<DictType> page1 = new Page<>(page, limit);
        IPage<DictType> resultPage = dictTypeService.getAll(page1, type);
        return resultPage;
    }

    @RequestMapping("/deleteDictType")
    public boolean deleteDictType(@RequestBody Map<String, Object> data){
        Integer id = (Integer) data.get("id");
        return dictTypeService.deleteType(id);
    }

    @RequestMapping("/getAllDictionaryByType")
    public List<Dictionary> getAllDictionaryByType(String type){
        return dictionaryService.getAllDictionary(type);
    }

    @RequestMapping("/saveDictionary")
    public boolean saveDictionary(@RequestBody DictionaryQueryDTO dto) {
        int id = dto.getId();
        String type = dto.getType();
        List<Dictionary> list = dto.getDetailList();
        return dictionaryService.saveDictionary(id, type, list);
    }

    @RequestMapping("/checkDictionaryTypeExists")
    public boolean checkDictionaryTypeExists(String type){
        return dictTypeService.checkDictionaryTypeExists(type);
    }





}
